JSGF.Audio = JSGF.inherit(JSGF.Object);

JSGF.Audio.prototype.init = function(file) {

	this.type = "JSGF.Audio";
	this.audio = new Audio();
	this.audio.src = null;
	this.audio.volume = 1.0;
	this.audio.autoplay = false;
	this.audio.loop = false;
	this.audio.preload = "auto";
	this.audio.controls = false;
	if (file != null) {
		this.setFile(file);
	}
	var self = this;
	this.audio.addEventListener("ended", function() {
		self.onPlayEnd();
	}, false);
}

////////////////////////////////////////
//Public
////////////////////////////////////////

JSGF.Audio.prototype.setFile = function(file) {
	var fileArray = file.split(".");
	var fileKind = "." + fileArray[fileArray.length - 1].toLowerCase();
	if (fileKind == ".ogg" || fileKind == ".mp3" || fileKind == ".wav" || fileKind == ".mp4") {
		this.audio.src = file;
	} else {
		var canPlayOgg = ("" != this.audio.canPlayType("audio/ogg"));
		var canPlayMp3 = ("" != this.audio.canPlayType("audio/mpeg"));
		var canPlayMp4 = ("" != this.audio.canPlayType("audio/aac"));
		if(canPlayOgg) {
			this.audio.src = file + ".ogg";
		} else if(canPlayMp3) {
			this.audio.src = file + ".mp3";
		} else if(canPlayMp4) {
			this.audio.src = file + ".mp4";
		}
	}
	//this.audio.load();
}

JSGF.Audio.prototype.setVolume = function(vol) {
	if (vol >= 0.1 && val <= 1.0) {
		this.audio.volume = vol;
	}
}

JSGF.Audio.prototype.getVolume = function() {
	return this.audio.volume;
}

JSGF.Audio.prototype.setLoop = function(loop) {
	this.audio.loop = loop;
}

JSGF.Audio.prototype.setTime = function(t) {
	this.audio.currentTime = t;
}

JSGF.Audio.prototype.getTime = function() {
	return this.audio.currentTime;
}

JSGF.Audio.prototype.play = function() {
	this.audio.play();
}

JSGF.Audio.prototype.pause = function() {
	this.audio.pause();
}

JSGF.Audio.prototype.stop = function() {
	if (!this.audio.ended) {
		this.audio.pause();
		this.audio.currentTime = 0;
	}
}

////////////////////////////////////////
// definition by user
////////////////////////////////////////
JSGF.Audio.prototype.onPlayEnd = function() {

}


